#coding:utf8
import tkinter as tk
from tkinter import StringVar, messagebox
from tkinter.filedialog import  askopenfilenames
from analytical.dif_classifier_fease import dif_method
from analytical.dif_dimension import dif_dimension
from analytical.test_classifier import test_classifier
from analytical.save_classifier import save_classifier
import os

class Classifier(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.filenames = ""
        self.grid()
        self.createWidgets()

    
    def createWidgets(self):
        self.lb = tk.Label(self,text = '请选择分词后的文件')
        self.lb.grid(row=0,column=0,columnspan=3)
        self.btfile = tk.Button(self,text="选择文件",width="20",command=self.comn)
        self.btfile.grid(row=1,column=0,columnspan=3)
        self.lb1 = tk.Label(self,text = '请选择分类器')
        self.lb1.grid(row=2,column=0,columnspan=3)
        self.lb2 = tk.Label(self,text = '请选择特征方法')
        self.lb2.grid(row=5,column=0,columnspan=3)
        self.vf1 = tk.IntVar() #分类器
        self.vf2 = tk.IntVar()
        self.vf3 = tk.IntVar()
        self.vf4 = tk.IntVar()
        self.vf5 = tk.IntVar()
        self.vf6 = tk.IntVar()
        self.checkboxf1 = tk.Checkbutton(self,text="BernoulliNB",variable=self.vf1)
        self.checkboxf2 = tk.Checkbutton(self,text="MultinomialNB",variable=self.vf2)
        self.checkboxf3 = tk.Checkbutton(self,text="LogisticRegression",variable=self.vf3)
        self.checkboxf4 = tk.Checkbutton(self,text="SVC",variable=self.vf4)
        self.checkboxf5 = tk.Checkbutton(self,text="LinearSVC",variable=self.vf5)
        self.checkboxf6 = tk.Checkbutton(self,text="NuSVC",variable=self.vf6)
        self.checkboxf1.grid(row=3,column=0,sticky=tk.W)
        self.checkboxf2.grid(row=3,column=1,sticky=tk.W)
        self.checkboxf3.grid(row=3,column=2,sticky=tk.W)
        self.checkboxf4.grid(row=4,column=0,sticky=tk.W)
        self.checkboxf5.grid(row=4,column=1,sticky=tk.W)
        self.checkboxf6.grid(row=4,column=2,sticky=tk.W)
        self.vm1 = tk.IntVar() #方法
        self.vm2 = tk.IntVar()
        self.vm3 = tk.IntVar()
        self.vm4 = tk.IntVar()
        self.vm5 = tk.IntVar()
        self.vm6 = tk.IntVar()
        self.checkboxm1 = tk.Checkbutton(self,text="所有词",variable=self.vm1)
        self.checkboxm2 = tk.Checkbutton(self,text="双词",variable=self.vm2)
        self.checkboxm3 = tk.Checkbutton(self,text="双词和所有词",variable=self.vm3)
        self.checkboxm4 = tk.Checkbutton(self,text="所有词丰富度",variable=self.vm4)
        self.checkboxm5 = tk.Checkbutton(self,text="双词丰富度",variable=self.vm5)
        self.checkboxm6 = tk.Checkbutton(self,text="所有词和双词丰富度",variable=self.vm6)
        self.checkboxm1.grid(row=6,column=0,sticky=tk.W)
        self.checkboxm2.grid(row=6,column=1,sticky=tk.W)
        self.checkboxm3.grid(row=6,column=2,sticky=tk.W)
        self.checkboxm4.grid(row=7,column=0,sticky=tk.W)
        self.checkboxm5.grid(row=7,column=1,sticky=tk.W)
        self.checkboxm6.grid(row=7,column=2,sticky=tk.W)
        self.btcl1 = tk.Button(self,text="不同特征方法下训练分类器",width="20",command=self.cfun1)
        self.btcl1.grid(row=8,column=0,columnspan=3)
        self.lb3 = tk.Label(self,text = '请填入维度：')
        self.lb3.grid(row=9,column=0)
        self.e = StringVar()
        self.entrydis = tk.Entry(self,textvariable=self.e,width="38")
        self.e.set("1000,1250,1750,2000,2250,2500,2750,3000")
        self.entrydis.grid(row=9,column=1,columnspan=2,sticky=tk.W)
        self.btcl2 = tk.Button(self,text="不同维度下训练分类器",width="20",command=self.cfun2)
        self.btcl2.grid(row=10,column=0,columnspan=3)
        self.btcl2 = tk.Button(self,text="测试最终分类器准确度",width="20",command=self.tfun3)
        self.btcl2.grid(row=11,column=0,columnspan=3)
        self.btcl2 = tk.Button(self,text="保存最终分类器",width="20",command=self.tfun4)
        self.btcl2.grid(row=12,column=0,columnspan=3)
    def comn(self):
        data_dir = os.path.dirname(os.path.dirname(os.getcwd()))+'\\data'
        self.filenames = askopenfilenames(initialdir = data_dir,title = "选择文件",filetypes = (("pkl files","*.pkl"),("all files","*.*")))
        if len(self.filenames) == 2:
            tmp_name = self.filenames[0].split("/")[-1]            
            string_filename =""
            if tmp_name[0:3] == "neg":
                self.neg_filename = str(self.filenames[0])
                self.pos_filename = str(self.filenames[1])
            if tmp_name[0:3] == "pos":
                self.neg_filename = str(self.filenames[1])
                self.pos_filename = str(self.filenames[0])   
            string_filename = str(self.filenames[0])+"\n" + str(self.filenames[1])
            self.lb.config(text = "您选择的文件是："+string_filename)
        elif len(self.filenames) == 0:
            self.lb.config(text = "您没有选择任何文件")
        else:
            self.lb.config(text = "选择文件数量不正确")    
            
    def cfun1(self):
        if len(self.filenames) != 2 :
            messagebox.showinfo("提示", "文件数量选择错误！")
        else:    
            self.f = [self.vf1.get(),self.vf2.get(),self.vf3.get(),self.vf4.get(),self.vf5.get(),self.vf6.get()]
            self.list1 = []
            self.list2 = []
            list1_tmp = ["BernoulliNB","MultinomialNB","LogisticRegression","SVC","LinearSVC","NuSVC"]
            for i in range(len(self.f)):
                if self.f[i] == 1:
                    self.list1.append(list1_tmp[i])
            self.m = [self.vm1.get(),self.vm2.get(),self.vm3.get(),self.vm4.get(),self.vm5.get(),self.vm6.get()]
            list2_tmp = ["bag_of_words","bigram","bigram_words","best_word_features","best_bigram_features","best_word_bigram_features"]
            for i in range(len(self.m)):
                if self.m[i] == 1:
                    self.list2.append(list2_tmp[i])
            self.f = []
            self.m = []
            dif_method(self.pos_filename,self.neg_filename,self.list1,self.list2) 
        
    def cfun2(self):
        if len(self.filenames) != 2 :
            messagebox.showinfo("提示", "文件数量选择错误！")
        else:
            self.f = [self.vf1.get(),self.vf2.get(),self.vf3.get(),self.vf4.get(),self.vf5.get(),self.vf6.get()]
            self.list1 = []
            self.list2 = []
            list1_tmp = ["BernoulliNB","MultinomialNB","LogisticRegression","SVC","LinearSVC","NuSVC"]
            for i in range(len(self.f)):
                if self.f[i] == 1:
                    self.list1.append(list1_tmp[i])
            self.m = [self.vm1.get(),self.vm2.get(),self.vm3.get(),self.vm4.get(),self.vm5.get(),self.vm6.get()]
            list2_tmp = ["bag_of_words","bigram","bigram_words","best_word_features","best_bigram_features","best_word_bigram_features"]
            for i in range(len(self.m)):
                if self.m[i] == 1:
                    self.list2.append(list2_tmp[i])
            
            self.dimension = self.e.get()
            if len(self.list2) == 0:
                messagebox.showinfo("提示", "请选择一个丰富度特征选择方法")
                return
            elif len(self.list2) > 1:
                messagebox.showinfo("提示", "请选择一个丰富度特征选择方法")
                return
            elif len(self.list2) == 1:
                for i in self.list2:
                    if i not in ["best_word_features","best_bigram_features","best_word_bigram_features"]:
                        messagebox.showinfo("提示", "请选择一个丰富度特征选择方法")
                        return
            self.best_fea = self.list2 #最佳特征选择方法
            self.f = []
            self.m = []
            dif_dimension(self.pos_filename,self.neg_filename,self.list1,self.best_fea,self.dimension)   
    
    def tfun3(self):
        if len(self.filenames) != 2 :
            messagebox.showinfo("提示", "文件数量选择错误！")
        else:
            self.f = [self.vf1.get(),self.vf2.get(),self.vf3.get(),self.vf4.get(),self.vf5.get(),self.vf6.get()]
            self.list1 = []
            self.list2 = []
            list1_tmp = ["BernoulliNB","MultinomialNB","LogisticRegression","SVC","LinearSVC","NuSVC"]
            for i in range(len(self.f)):
                if self.f[i] == 1:
                    self.list1.append(list1_tmp[i])
                        
            if len(self.list1) == 0:
                messagebox.showinfo("提示", "请选择最佳分类器")
                return
            elif len(self.list1) > 1:
                messagebox.showinfo("提示", "请选择最佳分类器")
                return    
            
            self.best_cla_name = self.list1 #最佳分类器  
            self.f = []
            
            self.m = [self.vm1.get(),self.vm2.get(),self.vm3.get(),self.vm4.get(),self.vm5.get(),self.vm6.get()]
            list2_tmp = ["bag_of_words","bigram","bigram_words","best_word_features","best_bigram_features","best_word_bigram_features"]
            for i in range(len(self.m)):
                if self.m[i] == 1:
                    self.list2.append(list2_tmp[i])
            
            if len(self.list2) == 0:
                messagebox.showinfo("提示", "请选择一个特征选择方法")
                return
            elif len(self.list2) > 1:
                messagebox.showinfo("提示", "请选择一个特征选择方法")
                return
            self.m = []
            self.best_fea = self.list2 #最佳特征选择方法
            
            self.dimension = self.e.get()
            if len(self.dimension.split(",")) == 0:
                messagebox.showinfo("提示", "请输入一个大于0的维度")
                return
            elif len(self.dimension.split(",")) > 1:
                messagebox.showinfo("提示", "请输入一个大于0的维度")
                return
            elif int(self.dimension.split(",")[0]) <= 0:
                messagebox.showinfo("提示", "请输入一个大于0的维度")
                return
            
            self.best_di = self.dimension.split(",")[0]
            
            messagebox.showinfo("测试结果",test_classifier(self.pos_filename,self.neg_filename,self.best_fea,self.best_cla_name,self.best_di))
    def tfun4(self):
        if len(self.filenames) != 2 :
            messagebox.showinfo("提示", "文件数量选择错误！")
        else:
            self.f = [self.vf1.get(),self.vf2.get(),self.vf3.get(),self.vf4.get(),self.vf5.get(),self.vf6.get()]
            self.list1 = []
            self.list2 = []
            list1_tmp = ["BernoulliNB","MultinomialNB","LogisticRegression","SVC","LinearSVC","NuSVC"]
            for i in range(len(self.f)):
                if self.f[i] == 1:
                    self.list1.append(list1_tmp[i])
                        
            if len(self.list1) == 0:
                messagebox.showinfo("提示", "请选择最佳分类器")
                return
            elif len(self.list1) > 1:
                messagebox.showinfo("提示", "请选择最佳分类器")
                return    
            
            self.best_cla_name = self.list1 #最佳分类器  
            self.f = []
            
            self.m = [self.vm1.get(),self.vm2.get(),self.vm3.get(),self.vm4.get(),self.vm5.get(),self.vm6.get()]
            list2_tmp = ["bag_of_words","bigram","bigram_words","best_word_features","best_bigram_features","best_word_bigram_features"]
            for i in range(len(self.m)):
                if self.m[i] == 1:
                    self.list2.append(list2_tmp[i])
            
            if len(self.list2) == 0:
                messagebox.showinfo("提示", "请选择一个特征选择方法")
                return
            elif len(self.list2) > 1:
                messagebox.showinfo("提示", "请选择一个特征选择方法")
                return
            self.m = []
            self.best_fea = self.list2 #最佳特征选择方法
            
            self.dimension = self.e.get()
            if len(self.dimension.split(",")) == 0:
                messagebox.showinfo("提示", "请输入一个大于0的维度")
                return
            elif len(self.dimension.split(",")) > 1:
                messagebox.showinfo("提示", "请输入一个大于0的维度")
                return
            elif int(self.dimension.split(",")[0]) <= 0:
                messagebox.showinfo("提示", "请输入一个大于0的维度")
                return
            
            self.best_di = self.dimension.split(",")[0]        
            save_classifier(self.pos_filename,self.neg_filename,self.best_fea,self.best_cla_name,self.best_di)             